$(function () {
	$("#jqGrid").Grid({
		url: '../sys/merchant/list',
		colModel: [
			{label: '用户ID', name: 'userId', index: "user_id", key: true, hidden: true},
			{label: '用户名', name: 'username', width: 75},
			{label: '邮箱', name: 'email', width: 90},
			{label: '手机号', name: 'mobile', width: 100},
			{label: '商家编号', name: 'merchantId', width: 40},
			{label: '商家名称', name: 'merchantName', width: 75},
			{
				label: '状态', name: 'status', width: 80, formatter: function (value) {
					return value === 0 ?
						'<span class="label label-danger">待审核</span>' :
						'<span class="label label-success">审核通过</span>';
				}
			},
			{label: '创建者编号', name: 'createUserId', width: 40},
			{label: '备注', name: 'remark', width: 40},
			{
				label: '创建时间', name: 'createDate', index: "create_date", width: 80, formatter: function (value) {
					return transDate(value);
				}
			}]
	});
});

var setting = {
	data: {
		simpleData: {
			enable: true,
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url: "nourl"
		}
	}
};
var ztree;

var vm = new Vue({
	el: '#rrapp',
	data: {
		q: {
			username: null
		},
		showList: true,
		title: null,
		roleList: {},
		user: {
			status: 1,
			deptName: '',
			roleIdList: []
		},
		ruleValidate: {
			username: [
				{required: true, message: '姓名不能为空', trigger: 'blur'}
			],
			email: [
				{required: true, message: '邮箱不能为空', trigger: 'blur'},
				{type: 'email', message: '邮箱格式不正确', trigger: 'blur'}
			],
			mobile: [
				{required: true, message: '手机号不能为空', trigger: 'blur'}
			],
			merchantName: [
				{required: true, message: '商家名称不能为空', trigger: 'blur'}
			]
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增(默认密码：888888)";
			vm.roleList = {};
			vm.user = {status: 1, roleIdList: []};

			//获取角色信息
			this.getRoleList();
		},
		update: function () {
			var userId = getSelectedRow("#jqGrid");
			if (userId == null) {
				return;
			}

			vm.showList = false;
			vm.title = "修改";

			Ajax.request({
				url: "../sys/merchant/info/" + userId,
				async: true,
				successCallback: function (r) {
					vm.user = r.user;
					vm.user.password = "";
					//获取角色信息
					vm.getRoleList();
				}
			});

		},
		del: function () {
			var userIds = getSelectedRows("#jqGrid");
			if (userIds == null || userIds.length>1) {
				return;
			}
			Ajax.request({
				url: "../sys/merchant/info/" + userIds[0],
				async: true,
				successCallback: function (r) {
					vm.user = r.user;
					vm.user.password = "";
					//获取角色信息
					vm.getRoleList();
				}
			});
			confirm('确定要删除选中的记录？', function () {
				Ajax.request({
					url: "../sys/merchant/delete",
					params: JSON.stringify(userIds),
					contentType: "application/json",
					type: 'POST',
					successCallback: function () {
						alert('操作成功', function (index) {
							vm.reload();
						});
					}
				});
				Ajax.request({
					url: "../product/delete-merchant?merchantId="+vm.user.merchantId,
					contentType: "application/json",
					successCallback: function () {
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			console.log(vm.user.merchantId);
			if(!vm.user.merchantId){
				iview.Message.error("请输入商户编号");
				return;
			}
			var url = vm.user.userId == null ? "../sys/merchant/save" : "../sys/merchant/update";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.user),
				contentType: "application/json",
				type: 'POST',
				successCallback: function () {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		getRoleList: function () {
			Ajax.request({
				url: '../sys/role/select',
				async: true,
				successCallback: function (r) {
					vm.roleList = r.list;
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {'username': vm.q.username},
				page: page
			}).trigger("reloadGrid");
			vm.handleReset('formValidate');
		},
		reloadSearch: function() {
			vm.q = {
				username: '',
			}
			vm.reload();
		},
		handleSubmit: function (name) {
			handleSubmitValidate(this, name, function () {
				vm.saveOrUpdate()
			});
		},
		handleReset: function (name) {
			handleResetForm(this, name);
		}
	}
});